<?php
class Pageley_Serializer_Converter_Number extends Pageley_Serializer_Converter_Default {
    
    public function convertFromFormat($value, $format = null, $serializedName = null) {
        $value = parent::convertFromFormat($value);
        if($value !== null && $format !== null) {
            $decimalPoint = Pageley_Util_String::substring($format, 1, 1);
            $value = Pageley_Util_String::replace($value, $decimalPoint, ".");
            if(is_numeric($value) && Pageley_Util_String::contains($value, ".")) {
                $maxDecimals = Pageley_Util_String::length(Pageley_Util_String::substringAfter($format, $decimalPoint));
                $decimals = Pageley_Util_String::length(Pageley_Util_String::substringAfter($value, "."));
                if($decimals > $maxDecimals) {
                    $value = round($value, $maxDecimals);
                }
            }
        }
        return $value;
    }
}